package com.chookss.mine.personal;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.chookss.R;
import com.chookss.Urls;
import com.chookss.base.BaseViewHolderEx;
import com.chookss.mine.entity.MyViedeoEntity;
import com.chookss.tools.MyToast;
import com.chookss.tools.NavigationTools;
import com.chookss.tools.SignUtils2;
import com.chookss.tools.Utils;
import com.chookss.tools.okgoconfig.MyHttpRequest;
import com.chookss.tools.okgoconfig.OkGoUtil;
import com.chookss.tools.okgoconfig.callback.JsonCallback;
import com.chookss.view.RecycleViewDivider;
import com.johnrambo.ktea.common.klogutil.KLog;
import com.lvgroup.hospital.base.BaseAct;
import com.lzy.okgo.model.Response;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.annotations.Nullable;

public class VideoSetActivity extends BaseAct {


    @BindView(R.id.common_title_txt)
    TextView commonTitleTxt;
    @BindView(R.id.tvNone)
    TextView tvNone;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private MySetAdapter adapter;
    private List<MyViedeoEntity> list = new ArrayList<>();
    private  int index=-1;//
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        setContentView(R.layout.activity_video_set);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        initState();
        commonTitleTxt.setText("我的集合");
        NavigationTools.saveUseRecord(this, "1", "19");
        list= (List<MyViedeoEntity>) getIntent().getSerializableExtra("lists");
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        recyclerView.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.HORIZONTAL, Utils.dip2px(this, 0.5f), getResources().getColor(R.color.line_gray)));
        adapter = new MySetAdapter(R.layout.item_video_set, this, list);
        recyclerView.setAdapter(adapter);
    }

    class MySetAdapter extends BaseQuickAdapter<MyViedeoEntity, BaseViewHolderEx> {
        private Context context;

        public MySetAdapter(int layoutResId, Context context, @Nullable List<MyViedeoEntity> data) {
            super(layoutResId, data);
            this.context = context;
        }

        @Override
        protected void convert(BaseViewHolderEx helper, MyViedeoEntity item) {

            TextView tvName = helper.getView(R.id.tvName);
            LinearLayout llDelete = helper.getView(R.id.llDelete);
            LinearLayout llEdit = helper.getView(R.id.llEdit);
            tvName.setText(item.getCollectionsName());
            llDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    index=-1;
                    if(item.getVideoList().size()>0){
                        MyToast.show("集合下面删除全部视频后可以删除");
                    }else{
                        deleteData(item.getCollectionsCode(),helper.getItemPosition());
                    }
                }
            });
            llEdit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent  intent = new Intent(VideoSetActivity.this, VideoSetEditActivity.class);
                    intent.putExtra("index", helper.getItemPosition());
                    intent.putExtra("collectionsCode",item.getCollectionsCode());
                    intent.putExtra("collectionsName",item.getCollectionsName());
                    startActivityForResult(intent, 1);
                }
            });

        }
    }

    private void deleteData(String collectionsCode,int position) {
        Map<String, String> allmap = new HashMap<>();
        allmap.put("collectionsCode", collectionsCode);
        MyHttpRequest.postRequest(Urls.deleteCollection, this, SignUtils2.makeSignStr(allmap), new JsonCallback<String>() {
            @Override
            public void onSuccess(Response<String> response) {
                try {
                    dismissLoading();
                    JSONObject jsonObject = new JSONObject(response.body().toString());
                    if ("200".equals(jsonObject.getString("code"))) {
                        list.remove(position);
                        adapter.notifyDataSetChanged();
                        MyToast.show("删除成功");
                        if(list.size()==0){
                            recyclerView.setVisibility(View.GONE);
                            tvNone.setVisibility(View.VISIBLE);
                        }
                    }else{
                        if (jsonObject.getString("code").startsWith("400")) {
                            OkGoUtil.onErrorIntent(jsonObject.getString("code"),jsonObject.getString("msg"));
                        }else{
                            MyToast.show(jsonObject.getString("msg"));
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @OnClick({R.id.common_title_back})
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.common_title_back:
                finish();
                break;
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @androidx.annotation.Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        KLog.i(requestCode + "--" + resultCode);

        if (requestCode == 1) {
            //资料修改
            if (data == null) {
                return;
            }
            switch (resultCode) {
                case 1:
                    String collectionsName = data.getStringExtra("collectionsName");
                    int index = data.getIntExtra("index",0);
                    list.get(index).setCollectionsName(collectionsName);
                    adapter.notifyItemChanged(index);
                    break;
            }
        }
    }
}
